from PIL import Image
import sys
import os
path = sys.argv[1]
fmt = sys.argv[2]
def cut_front(image_i,image_o):
    im = Image.open(image_i)
    x,y = im.size
    print(x,y)
    im_new = im.crop((0,0,x*0.55,y))
    print(im_new.size)

    im_new.save(image_o)
    

def cut_behind(image_i, image_o):
    im = Image.open(image_i)
    x, y = im.size
    print(x, y)
    im_new = im.crop((x*0.45, 0, x, y))
    print(im_new.size)
    im_new.save(image_o)


def get_imlist(path, fmt_input):
    return [os.path.join(path, f) for f in os.listdir(path) if f.endswith(fmt_input)]


def switch(argument):
    switcher = {
        "jpg": "JPG",
        "png": "PNG",
        "pgm": "PGM",
    }
    return switcher.get(argument, "nothing")


def main():
    im_list = im_list = get_imlist(path, fmt) + get_imlist(path, switch(fmt))
    for index in range(len(im_list)):
        cut_front(im_list[index], os.path.join(path , str(index * 2) + "." + fmt))
        cut_behind(im_list[index], os.path.join(path , str(index * 2 + 1) + "." + fmt))
        print(index)
        ##os.remove(im_list[index])

if __name__ == '__main__':

    main()

